<template>
  <div class="role-list">
    <el-card class="box-card">
      <!-- 搜索表单 -->
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="输入搜索">
            <el-input v-model="form.name" placeholder="角色名称" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
            type="primary"
            @click="onSubmit"
            :disabled="isLoading"
          >查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button @click="addRole">新增角色</el-button>
      <!-- 展示弹出框内容 -->
      <el-dialog :title="isEdit ? '编辑角色' : '添加角色'" :visible.sync="dialogFormVisible">
        <create-or-edit
          :is-edit="isEdit"
          :role-id="roleId"
          @success="handleSuccess"
          @cancel="handleCancel"
        ></create-or-edit>
      </el-dialog>
      <!-- 展示数据的表格 -->
      <el-table
        v-loading="isLoading"
        :data="role"
        style="width: 100%">
        <el-table-column
          type="index"
          label="编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          width="180">
        </el-table-column>
        <el-table-column
          label="添加时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dateFormat(that) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                size="mini"
                type="text"
                @click="$router.push({
                  name: 'alloc-menu',
                  params: {
                    roleId: scope.row.id
                  }
                })"
              >分配菜单</el-button>
              <el-button
                size="mini"
                type="text"
                @click="$router.push({
                  name: 'alloc-resource',
                  params: {
                    roleId: scope.row.id
                  }
                })"
              >分配资源</el-button>
            </div>
            <div>
              <el-button
                size="mini"
                type="text"
                @click="editRole(scope.row)"
              >编辑</el-button>
              <el-button
                size="mini"
                type="text"
                @click="deleteRole(scope.row)"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.current"
        :page-sizes="[10, 20, 50]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :disabled="isLoading">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import createOrEdit from './createOrEdit'
import { getRolePages, deleteRole } from '@/services/role'
export default {
  components: {
    createOrEdit
  },
  data () {
    return {
      that: this,
      form: {
        name: '',
        current: 1,
        size: 10
      },
      role: [],
      total: 0,
      isLoading: false,
      // 控制对话框显示
      dialogFormVisible: false,
      // 控制对话框是编辑还是新增
      isEdit: false,
      // 存储正在编辑的角色 ID
      roleId: ''
    }
  },
  created () {
    this.loadRoleList()
  },
  methods: {
    // 加载角色列表
    async loadRoleList () {
      this.isLoading = true
      const { data } = await getRolePages(this.form)
      if (data.code === '000000') {
        this.isLoading = false
        this.role = data.data.records
        this.total = data.data.total
      }
      // console.log(data)
    },
    // 删除角色
    deleteRole (row) {
      this.$confirm(`确认删除该角色 ${row.name}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 删除角色
        const { data } = await deleteRole(row.id)
        if (data.code === '000000') {
          // 删除成功，重新加载列表
          this.loadRoleList()
        }
        this.$message.success('删除角色成功！')
        // console.log(data)
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },
    // 添加角色
    addRole () {
      this.dialogFormVisible = true
      this.isEdit = false
    },
    // 编辑角色
    editRole (row) {
      this.dialogFormVisible = true
      this.isEdit = true
      this.roleId = row.id
    },
    updateMenu (row) {},
    updateResource (row) {},
    handleSizeChange (val) {
      this.form.size = val
      // 页面条数发生改变时，需要将当前页面还原为 1
      this.form.current = 1
      this.loadRoleList()
    },
    handleCurrentChange (val) {
      this.form.current = val
      this.loadRoleList()
    },
    onSubmit () {
      // 手动把当前页改成 1
      this.form.current = 1
      this.loadRoleList()
    },
    // 监听子组件添加角色确认按钮
    handleSuccess () {
      this.dialogFormVisible = false
      this.loadRoleList()
    },
    // 监听子组件取消按钮
    handleCancel () {
      this.dialogFormVisible = false
    },
    // 判断时间不足10的前面加0
    judgeDate (date) {
      if (date < 10) {
        return '0' + date
      } else {
        return date
      }
    }
  },
  filters: {
    dateFormat (date, that) {
      date = new Date(date)
      const year = date.getFullYear()
      const month = that.judgeDate(date.getMonth() + 1)
      const day = that.judgeDate(date.getDate())
      const hours = that.judgeDate(date.getHours())
      const minutes = that.judgeDate(date.getMinutes())
      const seconds = that.judgeDate(date.getSeconds())
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
